package com.lili.operate.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lili.model.operate.MessageTemplate;
import com.lili.operate.mapper.MessageTemplateMapper;
import com.lili.operate.service.MessageTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;

@Service
public class MessageTemplateServiceImpl implements MessageTemplateService {

    private final String PREFIX = "SMS_";

    @Autowired
    MessageTemplateMapper messageTemplateMapper;

    /**
     * 更新短信模板内容
     * @param messageTemplate
     * @return
     */
    @Override
    public int updateTemplate(MessageTemplate messageTemplate) {
        QueryWrapper<MessageTemplate> wrapper = new QueryWrapper<>();
        wrapper.eq("id",messageTemplate.getId());
        return messageTemplateMapper.update(messageTemplate,wrapper);
    }

    /**
     * 保存短信模板信息
     * @param messageTemplate
     * @return
     */
    @Override
    public int saveTemplate(MessageTemplate messageTemplate) {
        //生成编码
        long currentTimestampSeconds = Instant.now().getEpochSecond();
        String code = PREFIX + currentTimestampSeconds;
        messageTemplate.setTemplateCode(code);
        //生成编码
        return messageTemplateMapper.insert(messageTemplate);
    }
}
